@extends('common/main')
@section('title','用户管理')
@section('cnt')
@include('common/msg')
<button class="btn btn-warning">删除全部</button>
<a class="btn btn-primary" href="{{ route('add') }}">添加用户</a>
<table class="table table-hover">
    <thead>
        <tr>
            <th><input type="checkbox" name="" id="delall"></th>
            <th>ID</th>
            <th>用户名</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($data as $item)
        <tr>
            <td><input type="checkbox" name="ids[]" id="" value="{{$item->id}}"></td>
            <td scope="row">{{$item->id}}</td>
            <td>{{$item->username}}</td>
            <td>{{$item->email}}</td>
            <td>
                <a class="btn btn-xs btn-success" href="{{ route('edit',$item) }}">修改</a>
                <a class="btn btn-xs btn-danger" href="{{ route('del',$item) }}">删除</a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>
{{$data->links()}}
@endsection
@section('js')
<script>
    const _token = "{{csrf_token()}}";
    // 删除单个
    $('.btn-danger').click(function () {
        let url = $(this).attr('href');
        swal({
            title: '确认删除？',
            text: '您确认要删除此用户吗？',
            icon: 'warning',
            buttons: true,
            dangerMode: true,
        }).then((willDelete) => {
            if (willDelete) {
                $.ajax({
                    url,
                    type: 'DELETE',
                    data: {
                        _token
                    }
                }).then(ret => {
                    swal(ret.msg, {
                        icon: "success",
                    });
                });
            }
        });
        return false;
    });
    // 删除多个
    $('#delall').click(function () {
        let bool = $(this).prop('checked');
        $('input[name="ids[]"]').prop('checked', bool);
    });
    $('.btn-warning').click(function () {
        let ids = [];
        $('input[name="ids[]"]:checked').each((index, item) => {
            ids.push($(item).val());
        });
        $.ajax({
            url:"{{ route('deleteAll') }}",
            type: 'DELETE',
            data: {
                _token,ids
            }
        }).then(ret => {
            swal(ret.msg, {
                icon: "success",
            });
        });

    })
</script>
@endsection